package com.apimarket.entity.vo;

//import com.hdu.datagovermence.metadata.common.exception.ResultException;
//import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回前端的统一数据类型
 *
 * @author JHEP
 */
@Data
@NoArgsConstructor
public class JsonResult<T> implements Serializable {
    private static final long serialVersionUID = -7373554338313880705L;

    /**
     * 错误码.
     */
    //@ApiModelProperty(name = "code", value = "错误码")
    private Integer code;

    /**
     * 提示信息.
     */
    //@ApiModelProperty(name = "msg", value = "提示信息")
    private String msg;

    /**
     * 具体的内容.
     */
    //@ApiModelProperty(name = "data", value = "数据内容")
    private T data;

    public static <T> JsonResult<T> getSuccessRes(T data) {
        return new JsonResult(data);
    }

    public static JsonResult getFailRes(Integer code, String msg) {
        return new JsonResult(code, msg);
    }

//    public JsonResult(String message) {
//        this.msg = message;
//    }

    public JsonResult(T data) {
        this.code = 200;
        this.msg = "success";
        this.data = data;
    }

    public JsonResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public JsonResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

//    public JsonResult(Throwable e) {
////		code=500;//状态改为异常
//        msg = e.getMessage();
//    }

//    public static JsonResult<Object> fromException(ResultException e) {
//        JsonResult<Object> res = new JsonResult<Object>();
//        if(e.getCode()!=null) {
//            res.setCode(e.getCode());
//        }else {
//            res.setCode(0);
//        }
//        res.setMsg(e.getMsg());
//        res.setCode(e.getCode());
//        return res;
//    }
}
